/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LoggedInTeacherView.java
 *
 * Created on 2011-maj-23, 11:43:25
 */
package edu.chalmers.se.tda367.gui.view;

import java.awt.Color;
import java.beans.PropertyChangeSupport;
import edu.chalmers.se.tda367.controller.TestController;
import edu.chalmers.se.tda367.database.DatabaseController;
import edu.chalmers.se.tda367.gui.panel.ImagePanel;
import edu.chalmers.se.tda367.gui.panel.UserListPanel;
import edu.chalmers.se.tda367.gui.window.AddTeacherWindow;
import edu.chalmers.se.tda367.gui.window.AddUserWindow;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.FileNotFoundException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
import javax.swing.JPanel;
import edu.chalmers.se.tda367.main.Pluggis;
import edu.chalmers.se.tda367.model.User;
import edu.chalmers.se.tda367.observer.GlobalObserverList;
import java.io.IOException;

/**
 *
 * @author malinanker
 */
public class LoggedInTeacherView extends ImagePanel implements PropertyChangeListener {

    private PropertyChangeSupport pcs;

    /** Creates new form LoggedInTeacherView */
    public LoggedInTeacherView() {
        initComponents();

        this.setImage("database/pictures/Silhouette.png");
        teacherNameLabel.setText("Hej " + Pluggis.getInstance().getSelectedUser().getName() + "!");


        GlobalObserverList.getInstance().addObserver(this);
        this.setImage("database/pictures/Australia (488).JPG");
        jPanel2.add(new UserListPanel());
        teacherImage.setIcon(Pluggis.getInstance().getSelectedUser().getPanel().getPicture());
        Pluggis.repaintViews();
        this.validate();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        infoLabel = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        teacherNameLabel = new javax.swing.JLabel();
        logoutButton = new javax.swing.JButton();
        teacherImage = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        addTeacherButton = new javax.swing.JButton();
        AddUserButton = new javax.swing.JButton();
        deleteStudent = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(820, 550));
        setMinimumSize(new java.awt.Dimension(820, 550));
        setPreferredSize(new java.awt.Dimension(820, 550));

        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setOpaque(false);
        jPanel2.setPreferredSize(new java.awt.Dimension(800, 550));

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(edu.chalmers.se.tda367.main.Main.class).getContext().getResourceMap(LoggedInTeacherView.class);
        infoLabel.setFont(resourceMap.getFont("infoLabel.font")); // NOI18N
        infoLabel.setForeground(resourceMap.getColor("infoLabel.foreground")); // NOI18N
        infoLabel.setName("infoLabel"); // NOI18N

        jPanel3.setBackground(resourceMap.getColor("jPanel3.background")); // NOI18N
        jPanel3.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel3.setName("jPanel3"); // NOI18N
        jPanel3.setPreferredSize(new java.awt.Dimension(218, 259));

        teacherNameLabel.setFont(resourceMap.getFont("teacherNameLabel.font")); // NOI18N
        teacherNameLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        teacherNameLabel.setText(resourceMap.getString("teacherNameLabel.text")); // NOI18N
        teacherNameLabel.setName("teacherNameLabel"); // NOI18N

        logoutButton.setFont(resourceMap.getFont("logoutButton.font")); // NOI18N
        logoutButton.setText(resourceMap.getString("logoutButton.text")); // NOI18N
        logoutButton.setName("logoutButton"); // NOI18N
        logoutButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                logoutButtonActionPerformed(evt);
            }
        });

        teacherImage.setText(resourceMap.getString("teacherImage.text")); // NOI18N
        teacherImage.setName("teacherImage"); // NOI18N

        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .add(23, 23, 23)
                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel3Layout.createSequentialGroup()
                        .add(10, 10, 10)
                        .add(logoutButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 115, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel3Layout.createSequentialGroup()
                        .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(teacherImage, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE)
                            .add(teacherNameLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE))
                        .add(24, 24, 24))))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .add(teacherNameLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 22, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(teacherImage, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 158, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(logoutButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 44, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(6, 6, 6))
        );

        jPanel4.setName("jPanel4"); // NOI18N
        jPanel4.setOpaque(false);

        addTeacherButton.setFont(resourceMap.getFont("addTeacherButton.font")); // NOI18N
        addTeacherButton.setText(resourceMap.getString("addTeacherButton.text")); // NOI18N
        addTeacherButton.setName("addTeacherButton"); // NOI18N
        addTeacherButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addTeacherButtonActionPerformed(evt);
            }
        });

        AddUserButton.setFont(resourceMap.getFont("AddUserButton.font")); // NOI18N
        AddUserButton.setText(resourceMap.getString("AddUserButton.text")); // NOI18N
        AddUserButton.setName("AddUserButton"); // NOI18N
        AddUserButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddUserButtonActionPerformed(evt);
            }
        });

        deleteStudent.setFont(resourceMap.getFont("deleteStudent.font")); // NOI18N
        deleteStudent.setText(resourceMap.getString("deleteStudent.text")); // NOI18N
        deleteStudent.setName("deleteStudent"); // NOI18N
        deleteStudent.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteStudentActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel4Layout = new org.jdesktop.layout.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(AddUserButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, deleteStudent, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE)
                    .add(addTeacherButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .add(AddUserButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 76, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(addTeacherButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 75, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(deleteStudent, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 75, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 556, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(layout.createSequentialGroup()
                        .add(14, 14, 14)
                        .add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 189, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(41, Short.MAX_VALUE))
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(811, 811, 811)
                    .add(infoLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(714, 714, 714)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(150, 150, 150)
                        .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 907, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 259, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(647, 647, 647)
                    .add(infoLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(494, 494, 494)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void logoutButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logoutButtonActionPerformed
        try {
            Pluggis.getInstance().getMainView().removeAll();
            Pluggis.getInstance().getMainView().add(new LoginView());
            Pluggis.repaintViews();
        } catch (FileNotFoundException ex) {
        }
    }//GEN-LAST:event_logoutButtonActionPerformed

    private void deleteStudentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteStudentActionPerformed
        Pluggis g = Pluggis.getInstance();

        try {
            DatabaseController.removeUserfromDatabase(g.getSelectedUser());
            jPanel2.removeAll();// TODO add your handling code here:
            jPanel2.add(new UserListPanel());
            Pluggis.repaintViews();

        } catch (FileNotFoundException ex) {
        } catch (IOException ex) {
        }
    }//GEN-LAST:event_deleteStudentActionPerformed

    private void addTeacherButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addTeacherButtonActionPerformed
        new AddTeacherWindow().setVisible(true);
}//GEN-LAST:event_addTeacherButtonActionPerformed

    private void AddUserButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddUserButtonActionPerformed

        new AddUserWindow().setVisible(true);

    }//GEN-LAST:event_AddUserButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddUserButton;
    private javax.swing.JButton addTeacherButton;
    private javax.swing.JButton deleteStudent;
    private javax.swing.JLabel infoLabel;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JButton logoutButton;
    private javax.swing.JLabel teacherImage;
    private javax.swing.JLabel teacherNameLabel;
    // End of variables declaration//GEN-END:variables

    public void propertyChange(PropertyChangeEvent pce) {
        String change = pce.getPropertyName();

        if (change.equals("GUI.RepaintView")) {
            this.revalidate();
            this.repaint();
        }

        if (change == "GUI.SelectedUserChanged") {
            User u = (User) pce.getNewValue();

        }
    }
}
